import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class AppointListItem extends StatelessWidget {
  final String title;
  final String imageUrl;
  final int viewCount;
  static const double itemHeight = 100;
  static const double titleHeight = 80;
  static const double marginSize = 10;

  const AppointListItem(this.title, this.imageUrl, this.viewCount, {Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: const EdgeInsets.all(marginSize),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _imageWrapper(imageUrl),
          Expanded(
              child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _titleWrapper(context, title),
              _viewCountWrapper(viewCount.toString())
            ],
          ))
        ],
      ),
    );
  }

  Widget _titleWrapper(BuildContext context, String text) {
    return Container(
      height: titleHeight,
      margin: const EdgeInsets.fromLTRB(marginSize, 0, 0, 0),
      child: Text(
        title,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        style: Theme.of(context).textTheme.headline6,
      ),
    );
  }

  Widget _viewCountWrapper(String text) {
    return Container(
      margin: const EdgeInsets.fromLTRB(marginSize, 0, 0, 0),
      height: itemHeight - titleHeight,
      child: Row(
        children: [
          const Icon(
            Icons.remove_red_eye_outlined,
            size: 14.0,
            color: Colors.grey,
          ),
          const SizedBox(width: 5),
          Text(
            viewCount.toString(),
            style: const TextStyle(color: Colors.grey, fontSize: 14.0),
          ),
        ],
      ),
    );
  }

  Widget _imageWrapper(String imageUrl) {
    return SizedBox(
      width: 150,
      height: itemHeight,
      child: CachedNetworkImage(
          imageUrl: imageUrl,
          progressIndicatorBuilder: (context, url, downloadProgress) =>
              LinearProgressIndicator(value: downloadProgress.progress),
          errorWidget: (context, url, error) =>
              Image.asset('images/image-failed.png')),
    );
  }
}
